package com.disdr.datacenter;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.UnicodeUtil;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.impl.TextCodec;

import java.util.Date;

public class JwtToken加解密 {

    public static void main(String[] args) {
        main();
        //dd();
        //zz();

    }
    public static void zz(){
        //http://tdc.gymcity.com.cn/admin/index?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1bmlmeV9pZCI6MTAwMDAsInVzZXJfbmFtZSI6ImFkbWluIiwibG9naW5fdGltZSI6MTY5MjA2MzY0OCwibG9naW5faXAiOiIxNzIuMTguMC4xIiwidGltZV9vdXQiOjg2NDAwfQ.dFYLfFcJeL18sZy6apfxyzncO-5pw7mRYgUSIUCno6E

        String php_token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1bmlmeV9pZCI6MTAwMDAsInVzZXJfbmFtZSI6ImFkbWluIiwibG9naW5fdGltZSI6MTY5MjA2MzY0OCwibG9naW5faXAiOiIxNzIuMTguMC4xIiwidGltZV9vdXQiOjg2NDAwfQ.dFYLfFcJeL18sZy6apfxyzncO-5pw7mRYgUSIUCno6E";
        Claims php_claims = Jwts.parser().setSigningKey(TextCodec.BASE64.encode("wg_token")).parseClaimsJws(php_token).getBody();
        System.out.println(php_claims);
        //{unify_id=10000, user_name=admin, login_time=1692063648, login_ip=172.18.0.1, time_out=86400}
        System.out.println(php_claims.get("unify_id"));
        System.out.println(php_claims.get("user_name"));
        System.out.println(php_claims.get("login_time"));
        System.out.println(php_claims.get("login_ip"));
        System.out.println(php_claims.get("time_out"));
        Long ll = (Long.valueOf(php_claims.get("login_time").toString())+Long.valueOf(php_claims.get("time_out").toString())) * 1000;
        System.out.println(DateUtil.formatDate(new Date(Long.valueOf(ll))));
    }

    void qq(){
        String token = Jwts.builder().setClaims(null).signWith(SignatureAlgorithm.HS512, "wg_token").compact();

        Claims claims = Jwts.parser()
                .setSigningKey("wg_token")
                .parseClaimsJws(token)
                .getBody();

        System.out.println(claims.getSubject());
    }

    public static void dd(){
        String php_token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.Ilx1NjIxOFx1OTYxZjoyOjE2OTY3NDU5MTYi.zC4RAHARZMo9kqH-houVjYEXPKxmxsAfqm6bl7NOi8E";
        //Claims php_claims = Jwts.parser().setSigningKey(TextCodec.BASE64.encode("betterWe")).parseClaimsJws(php_token).getBody();
        String sss = Jwts.parser().setSigningKey(TextCodec.BASE64.encode("betterWe")).parsePlaintextJws(php_token).getBody();

        System.out.println(UnicodeUtil.toString(sss));
        System.out.println(new Date(1696745916000L));
    }

    public static void main() {
        //两种解码方式
        //String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.InRlbXBfdmlzaXRvcjotMToxNzM0MzM5NDIzIg.PROR9TMP8tYiNRtTZwSK9zTqX-rkdaucjWRePPpvW2w";
        //String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.Im9mZGYxdjRJZ2JzTzF4TVl1NTFsNk8zU2lDRlkzNjU6MjE0MTE5OjE3MzU4ODY3MDIi.liFqyfaGFkmoQ0lWpVxb8RQpw6RPVyfu6o2p0iaE9P0";
        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.Im9mZGYxdjBsNjFXNkZkQXhwaVBJZ2xVN1VLYjQ6MjAwMTU3OjE3NTQ2NDAxNjIi.YodnXQJ-afxrBITgev9dIYAHT4-4EGzPSn3iNyHqJnU";
        /*Claims claims = Jwts.parser()
                .setSigningKey(TextCodec.BASE64.encode("token_login"))
                .parseClaimsJws(token)
                .getBody();
        System.out.println(claims.getSubject());*/
        String sss = Jwts.parser().setSigningKey(TextCodec.BASE64.encode("token_login")).parsePlaintextJws(token).getBody();
        System.out.println(sss);

    }


    /*public void yy(){
        //java 出错误版

        JwtBuilder jwtBuilder = Jwts.builder();
        String token = jwtBuilder
                .claim("access_key","6uVG1xmibb3EX7XhUV3g6jflPidNhNon")
                .claim("header","MARKETING_MALL")
                .setExpiration(new Date(System.currentTimeMillis() + 1000*3600*24))
                .signWith(SignatureAlgorithm.HS256, "hj4iRrB2DEGAMDRHzVYFed14weSN")
                .compact();
        header.add("Authorization", "Bearer "+token);


        php解

                $result = JWT::decode("java的jwt token", “hj4iRrB2DEGAMDRHzVYFed14weSN”,['HS256']);


        理论上来说是没问题的 现实很残酷

        最终找了资料找到了原因  是格式转换问题。jjwt 需要使用 base64 编码的密钥，而 php-jwt 使用纯字符串

//java 正确写法
        String token = jwtBuilder
                .claim("access_key","6uVG1xmibb3EX7XhUV3g6jflPidNhNon")
                .claim("header","MARKETING_MALL")
                .setExpiration(new Date(System.currentTimeMillis() + 1000*3600*24))
                .signWith(SignatureAlgorithm.HS256, TextCodec.BASE64.encode("hj4iRrB2DEGAMDRHzVYFed14weSN"))
                .compact();


    }*/
}
